package com.oig.sys.auth.dao;

import com.oig.sys.auth.entity.TbResourceColumn;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

/**
@author wuxl
*/
public interface ITbResourceColumnDao extends JpaRepository<TbResourceColumn, Integer> {

	@Query("from TbResourceColumn r where r.id=:id")
	TbResourceColumn getRsColumnById(@Param("id") Integer id);

	TbResourceColumn getByResourceIdAndUserColumnIdAndColumnField(Integer resourceId, Integer userColumnId, String columnField);

	@Query("select c from TbResourceColumn c where c.resourceId=:resourceId and c.userColumnId=0 order by c.listSort")
	List<TbResourceColumn> findBaseColumnByResourceId(@Param("resourceId") Integer resourceId);

	@Query("select c from TbResourceColumn c where c.resourceId=:resourceId and c.userColumnId=:userColumnId  order by c.listSort")
	List<TbResourceColumn> findUserColumnByResourceId(@Param("resourceId") Integer resourceId, @Param("userColumnId") Integer userColumnId);

//	@Query("select c from TbResourceColumn c where c.resourceId=:resourceId and c.userColumnId=:userColumnId and c.code=:code")
//	TbResourceColumn getResourceColumn(@Param("resourceId") Integer resourceId,
//									   @Param("userColumnId") Integer userColumnId,
//									   @Param("code")String code);

	/**
	 * resourceId userColumnId code 唯一索引
	 */
	@Query("select c.columnField from TbResourceColumn c where c.id not in(:columnIdList) and c.resourceId=:resourceId and c.userColumnId=0")
	List<String> findDelColumnFieldById(@Param("columnIdList") List<Integer> columnIdList, @Param("resourceId") Integer resourceId);

	@Query("select c.id from TbResourceColumn c where c.resourceId=:resourceId and c.columnField in(:columnFieldList) ")
	List<Integer> findDelColumnIdsByCode(@Param("resourceId") Integer resourceId, @Param("columnFieldList") List<String> columnFieldList);

	@Modifying
	@Query("delete from TbResourceColumn c where c.id in(:columnIdList)")
	void deleteByIdList(@Param("columnIdList") List<Integer> columnIdList);

	@Modifying
	@Query("delete from TbResourceColumn c where c.resourceId=:resourceId")
	void deleteByResourceId(@Param("resourceId") Integer resourceId);


	@Modifying
	@Query("update TbResourceColumn r set " +
			" r.code=:#{#resourceColumn.code},"+
			" r.columnName=:#{#resourceColumn.columnName}," +
			" r.columnProperty=:#{#resourceColumn.columnProperty}," +
			" r.columnTableName=:#{#resourceColumn.columnTableName}," +
			" r.columnTable=:#{#resourceColumn.columnTable}," +
			" r.columnCol=:#{#resourceColumn.columnCol}," +
			" r.isPrimaryKey=:#{#resourceColumn.isPrimaryKey}," +
			" r.isOrderFlag=:#{#resourceColumn.isOrderFlag}," +
			" r.isChangeFlag=:#{#resourceColumn.isChangeFlag}," +
			" r.lastUpdateId=:#{#resourceColumn.lastUpdateId}," +
			" r.lastUpdateName=:#{#resourceColumn.lastUpdateName}," +
			" r.columnValueType=:#{#resourceColumn.columnValueType}," +
			" r.enumValueJson=:#{#resourceColumn.enumValueJson}," +
			" r.queryFlag=:#{#resourceColumn.queryFlag}," +
			" r.queryType=:#{#resourceColumn.queryType}," +
			" r.queryTypeApi=:#{#resourceColumn.queryTypeApi}" +
			" where r.id <> :#{#resourceColumn.id}" +
			"	and r.resourceId=:#{#resourceColumn.resourceId}" +
			" 	and r.columnField =:#{#resourceColumn.columnField} ")
	void updateResourceColumnByField(@Param("resourceColumn") TbResourceColumn resourceColumn);


	@Modifying
	@Query("update TbResourceColumn r set " +
			" r.isShowFlag=:#{#resourceColumn.isShowFlag}," +
			" r.showWidth=:#{#resourceColumn.showWidth}" +
			" where r.id <> :#{#resourceColumn.id}" +
			"	and r.resourceId=:#{#resourceColumn.resourceId}" +
			" 	and r.columnField =:#{#resourceColumn.columnField} ")
	void updateResourceColumnForNoChangeByField(@Param("resourceColumn") TbResourceColumn resourceColumn);

}
